<template>
  <el-dialog :title="'设置用户单位'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="登录账号">
            <span>{{ form.username }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名称">
            <span>{{ form.realname }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="所属单位">
        <el-select v-model="form.organId" :filter-method="_handleOrganFileter" filterable @blur="_handleOrganBlur" >
          <el-option v-for="item in organList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

// import { getUserById } from '@/api/systemManage/user'
import waves from '@/directive/waves' // Waves directive
import { mapGetters } from 'vuex'

import organApi from '@/api/systemManage/organ'

export default {
  directives: { waves },
  components: {
    // DetailCropper
  },

  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      visible: false,
      isEdit: false,
      form: {},
      formRules: {},
      organList: [],
      primaryOrganList: []
    }
  },

  computed: {
    ...mapGetters([
      'lang'
    ]),

    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },

  methods: {

    _formReset() {
      this.form = {
        userId: '',
        organId: ''
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleOrganFileter(input) {
      if (input) {
        this.organList = this.primaryOrganList.filter(t => t.name.indexOf(input) > -1)
      } else {
        this.organList = this.primaryOrganList
      }
    },

    _handleOrganBlur() {
      this.organList = this.primaryOrganList
    },

    _handleDialogOpen() {
      this._formReset()
      this.$emit('open')
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs['dataForm']
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params) {
      this.visible = true
      if (params) {
        this.isEdit = true
        organApi.getAll().then(resp => {
          this.form = params
          this.organList = resp.data || []
          this.primaryOrganList = JSON.parse(JSON.stringify(this.organList))
        }).catch(resp => {
          this.$message.error(resp.message)
        })
      } else {
        this.isEdit = false
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>
